package org.sky.common.web.api;

import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.sky.common.web.annotation.API;
import org.sky.common.web.util.ScanPackageUtil;
import org.springframework.web.bind.annotation.RequestMapping;

public class InterfaceURLScan {
	private Map<String,String> pathMap;
	
	public Map<String, String> getPathMap() {
		return pathMap;
	}

	public void setPathMap(Map<String, String> pathMap) {
		this.pathMap = pathMap;
	}

	public InterfaceURLScan(String packagePath){
		try{
			pathMap = new HashMap<String, String>();
			List<String> list =ScanPackageUtil.scanPackage(packagePath);
			for (Iterator iterator = list.iterator(); iterator.hasNext();) {
				String string = (String) iterator.next();
				Class<?> clazz = Class.forName(string);
				RequestMapping classAnnotation=clazz.getAnnotation(RequestMapping.class);
				Method[] methods = clazz.getDeclaredMethods();
				for (Method method : methods) {
					RequestMapping methodAnnotation = method.getAnnotation(RequestMapping.class);
					API apiAnnotation = method.getAnnotation(API.class);
					if(classAnnotation!=null && methodAnnotation!=null && apiAnnotation !=null){
						System.out.println("接口描述:"+apiAnnotation.name());
						System.out.println("接口路径:"+classAnnotation.value()[0]+methodAnnotation.value()[0]);
						pathMap.put(apiAnnotation.name(), classAnnotation.value()[0]+methodAnnotation.value()[0]);
					}
				}
			}
		}catch(Exception e){
			e.printStackTrace();
		}
	}
}
